<template>
  <div class="cinema-page">
    <div class="page-header canary-theme" v-if="cinemaInfo">
      <div class="page-header-left">
        <div class="page-header-back" @click="back"></div>
      </div>
      <div class="page-header-title line-ellipsis">
        {{ cinemaInfo.nm }}
      </div>
      <div class="page-header-right page-header-button">
        <div class="nav-btn">
          <img src="../assets/images/nav-button.png" alt="" class="nav-icon" />
        </div>
      </div>
    </div>
    <div class="cinema-block" v-if="cinemaInfo">
      <div class="cinema-info">
        <div class="title line-ellipsis">
          {{ cinemaInfo.nm }}
        </div>
        <div class="location line-ellipsis">
          {{ cinemaInfo.addr }}
        </div>
      </div>
    </div>
    <div class="movie-block">
      <div class="swiper-container swiper-container-cinema" ref="swiper">
        <div
          class="post-bg"
          v-if="movieslist"
          :style="{
            'background-image': 'url(' + movieslist[currentBg].img + ')',
          }"
        ></div>
        <div class="post-bg-filter"></div>
        <div
          class="swiper-wrapper"
          style="
            transition-duration: 0ms;
            transform: translate3d(137.5px, 0px, 0px);
          "
        >
          <div
            class="swiper-slide"
            v-for="(item, index) in movieslist"
            :key="index"
            @click="selectItem(item.id)"
          >
            <div
              class="post image-fade-in div-3Cvbv"
              :style="{ 'background-image': 'url(' + item.img + ')' }"
            ></div>
            <div></div>
            <div class="lazyload-wrapper"><div></div></div>
          </div>
        </div>
        <span
          class="swiper-notification"
          aria-live="assertive"
          aria-atomic="true"
        ></span>
      </div>
      <div class="movie-info-container" v-if="Currentmovie">
        <div class="movie-info">
          <div class="movie-title line-ellipsis">
            <span class="title">{{ Currentmovie.nm }}</span>
            <span class="grade" v-if="Currentmovie.sc != 0.0">
              <span class="score">
                {{ Currentmovie.sc }}<span class="small">分</span>
              </span>
            </span>
            <span class="grade" v-if="Currentmovie.sc == 0.0">
              <span class="score"> 暂无评分 </span>
            </span>
          </div>
          <div class="movie-desc line-ellipsis">
            {{ Currentmovie.desc }}
          </div>
        </div>
      </div>
    </div>
    <div class="show-wrap" v-if="showsInfo">
      <div class="date-nav mb-line-tb" v-if="datelist">
        <div
          :class="['date-item', { 'selected-date': currentday == index }]"
          v-for="(item, index) in datelist"
          :key="index"
          @click="currentday = index"
        >
          <div class="date-title">{{ navTime(item) }}</div>
        </div>
      </div>
      <div class="show-list-container" v-if="showsInfo[currentday].plist">
        <div
          class="show-item-wrap border-1px-t"
          v-for="(item, index) in showsInfo[currentday].plist"
          :key="index"
        >
          <div class="show-item">
            <div class="time-block">
              <div class="begin">{{ item.tm }}</div>
              <div class="end">
                {{ endTime(item.tm) }}
                <span class="tui">散场</span>
              </div>
            </div>
            <div class="info-block">
              <div class="lan">{{ item.lang }} {{ item.tp }}</div>
              <div class="hall">{{ item.th }}</div>
            </div>
            <div class="price">
              <div class="sellPr">
                <span class="d">￥</span>
                <span>
                  <span class="stonefont">{{ item.baseSellPrice }}</span>
                  <!-- {{item.sellPr}} -->
                </span>
              </div>
            </div>
            <div class="button-block">
              <div class="button" @click="buyTicket(item.seqNo)">购票</div>
            </div>
          </div>
        </div>
      </div>
      <div class="no-seat" v-if="showsInfo[currentday].plist.length == 0">
        <div class="icon today-no-movie"></div>
        <div class="text">今日场次已映完</div>
      </div>
    </div>
    <div class="lodaing" v-if="loading">
      <div class="img">
        <img class="img1" src="../assets/images/maoyan01.png" alt="" />
        <img class="img2" src="../assets/images/maoyan02.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import { getCinemasDetail, getCinemaShows } from "../api/cinema.js";
import Swiper from "swiper";
import { ref, nextTick, onMounted, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
export default {
  setup() {
    const router = useRouter();
    const route = useRoute();

    let cinemaInfo = ref(null); //影院信息
    let movieslist = ref(null); //正在上映电影
    let showsInfo = ref(null); //影片售卖信息
    let currentBg = ref(0);
    let Currentmovie = ref(null); //当前影片详情
    let datelist = ref([]); //日期
    let currentday = ref(0);
    let movieId = ref(null);
    let loading = ref(false); //加载动画
    let movieIndex = ref(route.query.movieId);
    let swiper = ref(null);

    //影院详情数据
    let getCinemaDetailFun = (id) => {
      getCinemasDetail({ cinemaId: id }).then((data) => {
        cinemaInfo.value = data.data;
      });
    };
    //影院正在上映电影  cinemaId 影院id  ci 城市id  channelId=4
    let getCinemaShowsFun = (id, ci) => {
      getCinemaShows({ cinemaId: id, ci, channelId: 4 }).then((data) => {
        // console.log(data);
        if (data) {
          setTimeout(() => {
            loading.value = false;
          }, 100);
        }
        movieslist.value = data.data.movies;
        Currentmovie.value = movieslist.value[currentBg.value];
        movieId.value = Currentmovie.value.id;
        showsInfo.value = Currentmovie.value.shows;
        // console.log(this.showsInfo);
        // console.log(this.movieId);
        showsInfo.value.forEach((item) => {
          if (item.showDate) {
            datelist.value = [...datelist.value, item.showDate];
          }
        });
        // console.log(movieIndex.value);
        // if (movieIndex.value) {
        //   getSlideToFun(route.query.movieId);
        // }
      });
    };
    //选择电影
    let selectItem = (id) => {
      let index = movieslist.value.findIndex((item) => item.id == id);
      currentBg.value = index;
      Currentmovie.value = movieslist.value[index];
      // console.log(this.movieslist);
    };
    // 返回
    let back = () => {
      router.go(-1);
    };
    //购票
    let buyTicket = (seqNo) => {
      let token = window.localStorage.getItem("token");
      if (!token) {
        router.push("/login");
      } else {
        router.push({
          path: "/seatselection",
          query: {
            seqNo,
            cinemaId: route.query.id,
            ci: route.query.ci,
            movieId: movieId.value,
            time: showsInfo.value[currentday.value].showDate,
          },
        });
      }
    };
    //自动跳转对应电影
    let getSlideToFun = (id) => {
      nextTick(()=>{
        setTimeout(() => {
          if (movieslist.value) {
          let index = movieslist.value.findIndex((item) => item.id == id);
          swiper.value.swiper.slideTo(index, 1000, true);
          Currentmovie.value = movieslist.value[index];
        }
      }, 500);
      })
    };
    // 监听背景图片
    watch(currentBg, () => {
      datelist.value = [];
      currentday.value = 0;
      getCinemaShowsFun(route.query.id, route.query.ci);
    });
    //散场时间
    let endTime = (value) => {
      if (value) {
        let starttime = value.split(":");
        let endTime = `${Number(starttime[0]) + 2}:${starttime[1]}`;
        if (Number(starttime[0]) + 2 == 24) {
          let endTime = `${Number(0) + "0"}:${starttime[1]}`;
          return endTime;
        }
        if (Number(starttime[0]) + 2 > 24) {
          let endTime = `${"0" + (Number(starttime[0]) + 2) - 24}:${
            starttime[1]
          }`;
          return endTime;
        }
        return endTime;
      }
    };
    //导航时间
    let navTime = (value) => {
      let dates = value;
      let datesar = dates.split("-");
      let date = new Date();
      let datewl = new Date(datesar[0], String(datesar[1] - 1), datesar[2]);
      let datewls = datewl.getDay();

      let datesl = date.getDate();
      if (datesar[2] == datesl) {
        return "今天" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datesar[2] == Number(datesl) + 1) {
        return "明天" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datesar[2] == Number(datesl) + 2) {
        return "后天" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datewls == 1) {
        return "星期一" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datewls == 2) {
        return "星期二" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datewls == 3) {
        return "星期三" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datewls == 4) {
        return "星期四" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datewls == 5) {
        return "星期五" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datewls == 6) {
        return "星期六" + datesar[1] + "月" + datesar[2] + "日";
      } else if (datewls == 0) {
        return "星期日" + datesar[1] + "月" + datesar[2] + "日";
      }
    };
    onMounted(() => {
      nextTick(() => {
        new Swiper(".swiper-container-cinema", {
          slidesPerView: "auto",
          spaceBetween: 0,
          observer: true, //自动初始化swiper对象
          observeParents: true, //自动初始化父节点swiper对象
          autoplayDisableOnInteraction: false,
          centeredSlides: true,
          slideToClickedSlide: true,

          on: {
            transitionEnd: function () {
              currentBg.value = this.activeIndex;
              // console.log(currentBg.value);
              // console.log(this.activeIndex);
              // this.Currentmovie=this.movieslist[this.activeIndex]
            },
          },
        });
        getSlideToFun(route.query.movieId);
      });
    });

    loading.value = true;
    // console.log($route.query);
    getCinemaDetailFun(route.query.id);
    getCinemaShowsFun(route.query.id, route.query.ci);
    movieIndex.value = route.query.movieId;
    return {
      cinemaInfo,
      movieslist,
      showsInfo,
      currentBg,
      Currentmovie,
      datelist,
      currentday,
      movieId,
      loading,
      movieIndex,
      selectItem,
      getCinemaShowsFun,
      getCinemaDetailFun,
      endTime,
      navTime,
      buyTicket,
      back,
      swiper
    };
  },
};
</script>

<style lang="less" scoped>
.cinema-page {
  width: 100%;
  height: 100%;
  overflow-y: scroll;
  background-color: #f0f0f0;

  .page-header {
    display: flex;
    height: 50px;
    text-align: center;
    color: #fff;
    background: #e54848;

    .page-header-left {
      width: 50px;
      height: 100%;
      line-height: 50px;

      .page-header-back {
        display: inline-flex;
        position: relative;
        width: 16px;
        height: 16px;
        vertical-align: middle;

        &::before {
          content: "";
          position: absolute;
          top: 0;
          width: 100%;
          height: 100%;
          box-sizing: border-box;
          border-bottom: 2px solid #fff;
          border-left: 2px solid #fff;
          transform: rotate(45deg);
        }
      }
    }
    .page-header-title {
      flex: 1;
      line-height: 50px;
      font-size: 18px;
      font-weight: 400;
    }
    .page-header-right {
      height: 100%;
      line-height: 50px;
    }
    .page-header-button {
      display: flex;
      position: relative;
      width: 100px;
      -webkit-box-align: center;
      align-items: center;

      .nav-btn {
        -webkit-box-flex: 1;
        flex: 1;
        text-align: right;
        margin-right: 6px;

        .nav-icon {
          width: 17px;
          height: 16px;
          display: inline-block;
        }
      }
    }
  }
  .cinema-block {
    background-color: #fff;

    .cinema-info {
      position: relative;
      padding: 15px;

      .title {
        font-size: 17px;
        line-height: 24px;
        font-weight: 700;
        color: #333;
      }
      .location {
        margin-top: 2px;
        font-size: 13px;
        line-height: 18.5px;
        color: #999;
      }
    }
  }
  .movie-block {
    overflow: hidden;

    .swiper-container {
      padding: 20px 15px 20px 5px;
      // transform: translateZ(0);

      .post-bg {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        filter: blur(30px);
        background-position-y: 40%;
        background-size: 100%;
        overflow: hidden;
        z-index: -1;
      }
      .post-bg-filter {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        background-color: #40454d;
        opacity: 0.55;
      }
      .swiper-wrapper {
        height: 95px !important;

        .swiper-slide {
          width: 65px;
          height: 95px;
          margin-left: 15px;
          box-sizing: initial;

          .post {
            width: 100%;
            height: 100%;
            background-color: #f5f5f5;
            // opacity: 0;
            background-size: cover;
            background-repeat: no-repeat;
          }
        }
        .swiper-slide-active {
          transform: scale(1.15);
          border: 2px solid #fff;

          ::after {
            content: "";
            position: absolute;
            bottom: -6px;
            left: 50%;
            transform: translateX(-50%);
            width: 10px;
            height: 5px;
            background-image: url("../assets/images/point.png");
            background-size: contain;
          }
        }
      }
    }
    .movie-info-container {
      height: 66.5px;
      background-color: #fff;

      .movie-info {
        padding: 11px 14px;
        text-align: center;

        .movie-title {
          height: 24px;
          line-height: 24px;
          font-size: 17px;
          color: #333;
          font-weight: 700;

          .title {
            margin-right: 5px;
          }
          .grade {
            color: #ffb400;
            font-size: 16px;

            .small {
              font-size: 10px;
            }
          }
        }
        .movie-desc {
          margin-top: 2px;
          height: 18.5px;
          line-height: 18.5px;
          font-size: 13px;
          color: #999;
        }
      }
    }
  }
  .show-wrap {
    background-color: #fff;

    .date-nav {
      height: 45px;
      width: 100%;
      white-space: nowrap;
      overflow-x: scroll;
      box-sizing: border-box;
      padding-right: 10px;

      .date-item {
        display: inline-block;
        line-height: 45px;
        height: 45px;
        margin-left: 30px;
        text-align: center;
        font-size: 14px;
        color: #666;
      }
      &::-webkit-scrollbar {
        display: none;
      }
      .selected-date {
        color: #f03d37;
        position: relative;

        &::after {
          content: "";
          position: absolute;
          width: 110%;
          height: 2px;
          bottom: 0;
          left: 50%;
          transform: translateX(-50%);
          background-color: #f03d37;
        }
      }
    }
    .mb-line-tb {
      background: url("../assets/images/线.png") 0 top repeat-x,
        url("../assets/images/线.png") 0 bottom repeat-x;
    }
    .show-list-container {
      .show-item-wrap {
        padding: 17px 12.5px;

        .show-item {
          display: flex;

          .time-block {
            position: relative;

            .begin {
              font-size: 20px;
              color: #333;
              line-height: 1;
              white-space: nowrap;
            }
            .end {
              margin-top: 10px;
              color: #999;
              font-size: 11px;
              line-height: 1;
              white-space: nowrap;
            }
          }
          .info-block {
            margin-left: 17px;
            flex: 1;

            .lan {
              margin-top: 2px;
              line-height: 18px;
              font-size: 13px;
              color: #333;
              white-space: normal;
            }
            .hall {
              margin-top: 7px;
              font-size: 11px;
              color: #999;
              white-space: normal;
            }
          }
          .price {
            flex: 0 1 auto;
            width: 130px;
            margin-left: 5px;

            .sellPr {
              display: inline-block;
              line-height: 1;
              color: #f03d37;
              margin-top: 1px;
              font-size: 19px;

              .d {
                font-size: 11px;
              }
              .stonefont {
                font-family: "stonefont";
              }
            }
          }
          .button-block {
            width: 45px;
            margin-left: 2px;
            position: relative;

            .button {
              position: absolute;
              top: 50%;
              transform: translateY(-50%);
              width: 100%;
              height: 25px;
              line-height: 26px;
              font-size: 12px;
              border: 1px solid #f06762;
              border-radius: 50px;
              color: #f03d37;
              text-align: center;
            }
          }
        }
      }
      .border-1px-t {
        position: relative;
        border: none;

        &::after {
          position: absolute;
          top: 0;
          left: 0;
          height: 1px;
          width: 100%;
          content: "";
          background: #d8d8d8;
          transform: scaleY(0.5);
          transform-origin: 0 0;
        }
      }
    }
    .no-seat {
      text-align: center;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -webkit-flex-direction: column;
      flex-direction: column;
      align-items: center;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
      justify-content: center;
      height: 230px;
      background-color: #f0f0f0;

      .icon {
        display: inline-block;
        margin: 50px auto 0;
        width: 77.5px;
        height: 71.5px;
      }
      .today-no-movie {
        background: url("../assets/images/today-no-show.3f026db3.png") no-repeat
          50%;
        background-size: contain;
      }
      .text {
        margin-top: 12px;
        line-height: 1;
        font-size: 16px;
        color: #acacac;
      }
    }
  }
  &::-webkit-scrollbar {
    display: none;
  }
}
@keyframes animation1 {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
.lodaing {
  width: 100%;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  background-color: #fff;
  z-index: 99;

  .img {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    margin: auto;
    width: 45px;
    height: 45px;

    img {
      position: relative;
      width: 100%;
      height: 100%;
      z-index: 3;
    }

    .img2 {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
      margin: auto;
      animation: animation1 1s linear 0s infinite;
    }
  }
}
</style>